﻿//========================================================= 
/**@file StringHelper.h 
 * @brief 字符串辅助函数 
 *
 * @date 2014-11-07   21:30:11 
 * @author Zhyioo 
 * @version 1.0
 */ 
//--------------------------------------------------------- 
#ifndef _LIBZHOUYB_STRINGHELPER_H_
#define _LIBZHOUYB_STRINGHELPER_H_
//--------------------------------------------------------- 
#include "../../include/Base.h"
//--------------------------------------------------------- 
namespace zhou_yb {
namespace extension {
namespace ability {
//--------------------------------------------------------- 
/// 字符串辅助 
class StringHelper
{
protected:
    StringHelper() {}
public:
    /// 分割字符串,返回分隔的子串个数 
    static size_t Split(const ByteArray& src, list_t<string>& _list, char flag = SPLIT_CHAR, bool ignore_empty = false)
    {
        size_t srclen = src.GetLength();
        if (srclen < 1)
            return 0;

        size_t count = 0;
        size_t pos = 0;
        for(size_t i = 0; i <= srclen; ++i)
        {
            if(i == srclen || src[i] == flag)
            {
                size_t copy_len = i - pos;
                if (copy_len > 0 || !ignore_empty)
                {
                    _list.push_back("");
                    if (copy_len > 0)
                    {
                        _list.back().assign(string(src.SubArray(pos).GetString(), copy_len));
                    }
                    ++count;
                }
                pos = i + 1;
            }
        }
        return count;
    }
    /// 组合字符串,将各个子串用join_str分隔开  
    static string Join(const list_t<string>& _list, const char* join_str = SPLIT_STRING)
    {
        string dst;
        list_t<string>::const_iterator itr;
        list_t<string>::const_iterator lastItr;

        lastItr = _list.end();
        --lastItr;

        string sJoin = _strput(join_str);
        for(itr = _list.begin(); itr != lastItr; ++itr)
        {
            dst += *itr;
            dst += sJoin;
        }

        return dst;
    }
    /// 组合IStringArguments中的字符串
    static string Join(const IStringArguments& arg, const char* join_str = SPLIT_STRING)
    {
        string sJoin = _strput(join_str);
        string sDst;
        IStringArguments::EnumeratorType enumer = arg.GetEnumerator();
        if(!enumer.MoveNext())
            return sDst;
        sDst += enumer.Current().Value;
        while(enumer.MoveNext())
        {
            sDst += sJoin;
            sDst += enumer.Current().Value;
        }
        return sDst;
    }
    /// 返回格式化后的目录路径(以\结尾)
    static void FormatDir(string& dir, const string& root = ".")
    {
        if(dir.length() < 1)
            dir = root;
        if(dir.length() < 1)
            dir = ".";
        if(dir[dir.length() - 1] != PATH_SEPARATOR)
            dir += PATH_SEPARATOR;
    }
};
//--------------------------------------------------------- 
} // namespace ability 
} // namespace extension 
} // namespace zhou_yb
//--------------------------------------------------------- 
#endif // _LIBZHOUYB_STRINGHELPER_H_
//========================================================= 